<?php
/*******************************************
	Images managment 
*******************************************/

// Avoid direct access
if(substr_count($_SERVER['PHP_SELF'], 'image.php') > 0) {
	header("HTTP/1.0 404 Not Found");
	exit;
}

class Image extends Curl{

	const SRC = "http://src.sencha.io/";	
	const DATA = "http://src.sencha.io/data/";	


	public static function get_image($img_src, $size="100", $data = true) {

		if(!$data) {
			$url = self::SRC;
			$url .= "x".$size."/".$img_src;
			return $url;
		}
	
		$url = self::SRC."x".$size."/".$img_src;
		$ch = parent::init();
		
		// use the device user agent
		if($_SERVER["HTTP_USER_AGENT"] != "") {
		
			// old mobile -> Mozilla/5.0 (SymbianOS/9.2; U; Series60/3.1 NokiaXxx/1.0; Profile/MIDP-2.0 Configuration/CLDC-1.1) AppleWebKit/413 (KHTML, like Gecko) Safari/413
			curl_setopt ($ch, CURLOPT_USERAGENT, $_SERVER["HTTP_USER_AGENT"]);
		}
		
		curl_setopt ($ch, CURLOPT_URL, $url);
		$result = curl_exec($ch);
		
		// If for some reason we can't get the image, return the same url of the original image
		if(curl_getinfo($ch,CURLINFO_HTTP_CODE) != 200) {
			
			curl_setopt ($ch, CURLOPT_URL, $img_src);
			$result = curl_exec($ch);

		}
		
		return $result;
	
	}

}

